﻿using lwzlz.Application.System;
using lwzlz.Core;
using lwzlz.Web.Areas.SysAdmin.Models;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace lwzlz.Web.Areas.SysAdmin.Controllers
{
    public class RoleController : AdminController
    {
        #region 首页
        public ActionResult Index()
        {
            return View();
        }
        #endregion

        #region 获取首页列表数据
        [HttpPost]
        public JsonResult GetList()
        {
            RoleService service = new RoleService();
            LayTableModel result = new LayTableModel();//layui表格返回数据格式
            try
            {
                int page = int.Parse(Request.Params["page"]);//当前页码
                int limit = int.Parse(Request.Params["limit"]);//每页数量

                #region 参数

                //参数
                string RoleName = Request.Params["RoleName"];//当前菜单id

                string sql = @"select * from Sys_Role ";
                string where = "WHERE IsDeleted = 0 ";

                //数据库参数
                Dictionary<string, object> param = new Dictionary<string, object>();

                //状态
                if (!string.IsNullOrWhiteSpace(RoleName))
                {
                    param.Add("@RoleName", RoleName);
                    where += " AND RoleName like '%'+@RoleName+'%' ";
                }
                #endregion

                //排序sql语句拼接
                string orderby = " ID DESC ";
                sql = sql + where;

                var list = service.GetDynamicPaged(page, limit, orderby, sql, param);
                var count = service.RecordQueryCount(sql, param);


                result.data = list;
                result.count = count;
            }
            catch (Exception ex)
            {
                result.code = -1;
                result.msg = ex.Message;
            }
            return Json(result, JsonRequestBehavior.DenyGet);
        }
        #endregion

        #region 添加和修改页面
        public ActionResult AddOrEditView(int ID = 0)
        {
            Role model = null;
            RoleService service = new RoleService();
            if (ID > 0)
            {
                model = service.Get(ID);
            }
            else
            {
                model = new Role();
                model.Id = 0;
            }
            return View(model);
        }
        #endregion

        #region 添加和修改行为
        [HttpPost]
        public JsonResult AddOrEdit(Role Model)
        {
            ResponseModel response = new ResponseModel();
            RoleService service = new RoleService();
            response.Success = false;
            int? count = 0;
            try
            {
                if (Model.Id > 0)
                {
                    Role model = service.Get(Model.Id);
                    model.RoleName = Model.RoleName;
                    model.RoleCode = Model.RoleCode;
                    model.RoleExplain = Model.RoleExplain;
                    model.RoleLevel = Model.RoleLevel;
                    model.LastModifierUserId = UserAuthentication.GetCurrentUser().UserID;
                    model.LastModificationTime = DateTime.Now;
                    count = service.UpDataSave(model);
                }
                else
                {
                    Model.CreatorUserId = UserAuthentication.GetCurrentUser().UserID;
                    Model.CreationTime = DateTime.Now;
                    Model.IsEnable = true;
                    count = service.Add(Model);
                }
                if (count > 0)
                {
                    response.Success = true;
                    response.Message = "操作成功";
                }
                else
                {
                    response.Success = false;
                    response.Message = "操作失败";
                }
            }
            catch (Exception ee)
            {
                response.Success = false;
                response.Message = "失败：" + ee.Message.ToString();
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }
        #endregion

        #region 设置权限角色
        public ActionResult SetPermissions(int ID)
        {
            var menus = new AuthorityService().GetRoleMenuTBs(ID);

            ViewBag.RoleID = ID;
            return View(menus);
        }
        #endregion

        #region 删除
        [HttpPost]
        public JsonResult DeleteList(string IdList = "")
        {
            ResponseModel response = new ResponseModel();
            RoleService serviece = new RoleService();

            try
            {
                serviece.DeleteList(IdList);
                response.Success = true;
                response.Message = "删除成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }
        #endregion

        #region 修改是否启用
        public JsonResult IsEnable(int ID, bool check)
        {
            ResponseModel response = new ResponseModel();
            RoleService service = new RoleService();
            Role model = service.Get(ID);
            try
            {
                model.IsEnable = (check == true ? true : false);
                model.LastModificationTime = DateTime.Now;
                model.LastModifierUserId = UserAuthentication.GetCurrentUser().UserID;
                int count = service.UpDataSave(model);
                if (count > 0)
                {
                    response.Success = true;
                    response.Message = "更新成功";
                }
                else
                {
                    response.Success = false;
                    response.Message = "更新失败";
                }

            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }
        #endregion

    }
}